package com.jr9z.jrjz.controller;

import com.github.pagehelper.PageHelper;
import com.jr9z.jrjz.common.BaseResponse;
import com.jr9z.jrjz.common.ResponseBuilder;
import com.jr9z.jrjz.entity.ClassicCase;
import com.jr9z.jrjz.entity.News;
import com.jr9z.jrjz.service.NewsService;
import com.jr9z.jrjz.utils.Constant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Author: MrZcc
 * @Description:新闻管理controller
 * @Date: Created in 10:18 2019-4-2
 * @Modified By:
 */

@Controller
@RequestMapping("/news")
public class NewsController {


    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private NewsService newsService;

    /**
     * 跳转到新闻发布
     *
     * @return
     */
    @RequestMapping("/newsAdd")
    public String newsAdd() {

        logger.info("进入新闻管理");

        return "news/newsAdd";

    }

    /**
     * 跳转到新闻管理
     *
     * @return
     */
    @RequestMapping("/newsManage")
    public String newsManage() {

        logger.info("进入新闻管理");

        return "news/newsManage";

    }

    /**
     * @Description:删除新闻
     * @date: 10:24 2019-4-2
     * @param: newsId
     * @return: com.jr9z.jrjz.common.BaseResponse
     */
    @RequestMapping(value = "/admin/deleteNews", method = RequestMethod.POST)
    @ResponseBody
    public BaseResponse deleteNewsById(Integer newsId) {

        int result = 0;
        ResponseBuilder custom = ResponseBuilder.custom();

        try {
            result = newsService.deleteByPrimaryKey(newsId);

            custom = Constant.RESULT(result);

        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }

        return custom.build();
    }


    /**
     * @Description:根据新闻id获取新闻详情
     * @date: 10:34 2019-4-2
     * @param: newsId 新闻id
     * @return: com.jr9z.jrjz.common.BaseResponse
     */
    @RequestMapping(value = "/getNewsById", method = RequestMethod.GET)
    @ResponseBody
    public BaseResponse getNewsDetail(Integer newsId) {

        News news;
        ResponseBuilder custom = ResponseBuilder.custom();

        try {
            news = newsService.selectByPrimaryKey(newsId);

            custom = Constant.OBJRESULT(news);

        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }

        return custom.build();
    }


    /**
     * @Description:根据新闻id获取新闻详情
     * @date: 10:34 2019-4-2
     * @param: newsId 新闻id
     * @return: com.jr9z.jrjz.common.BaseResponse
     */
    @RequestMapping(value = "/admin/getNewsById", method = RequestMethod.GET)
    @ResponseBody
    public BaseResponse adminGetNewsDetail(Integer newsId) {

        News news;
        ResponseBuilder custom = ResponseBuilder.custom();

        try {
            news = newsService.selectByPrimaryKey(newsId);

            custom = Constant.OBJRESULT(news);

        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }

        return custom.build();
    }


    /**
     * @Description:添加新闻
     * @date: 10:39 2019-4-2
     * @param: news json格式的新闻实体
     * @return: com.jr9z.jrjz.common.BaseResponse
     */
    @RequestMapping(value = "/admin/addNews", method = RequestMethod.POST)
    @ResponseBody
    public BaseResponse addNews(@RequestBody News news) {

        int result = 0;

        ResponseBuilder custom = ResponseBuilder.custom();

        try {
            result = newsService.insertSelective(news);

            custom = Constant.RESULT(result);

        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }

        return custom.build();

    }


    /**
     * @Description:修改新闻
     * @date: 10:39 2019-4-2
     * @param: news json格式的新闻实体
     * @return: com.jr9z.jrjz.common.BaseResponse
     */
    @RequestMapping(value = "/admin/updateNews", method = RequestMethod.POST)
    @ResponseBody
    public BaseResponse updateNews(@RequestBody News news) {

        int result = 0;

        ResponseBuilder custom = ResponseBuilder.custom();

        try {
            result = newsService.updateByPrimaryKeySelective(news);

            custom = Constant.RESULT(result);

        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }

        return custom.build();

    }

    /**
     * @Description:获取企业动态列表
     * @date: 11:25 2019-4-2
     * @param:
     * @return: com.jr9z.jrjz.common.BaseResponse
     */
    @RequestMapping(value = "/getTypeOne", method = RequestMethod.GET)
    @ResponseBody
    public BaseResponse getNewsTypeOne() {

        ResponseBuilder custom = ResponseBuilder.custom();

        List<News> newsList = new ArrayList<>();

        try {
            newsList = newsService.getNewsTypeOne();

            custom = Constant.OBJRESULT(newsList);

        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }

        return custom.build();
    }

    /**
     * @Description:获取企业动态列表
     * @date: 11:25 2019-4-2
     * @param:
     * @return: com.jr9z.jrjz.common.BaseResponse
     */
    @RequestMapping(value = "/admin/getTypeOne", method = RequestMethod.GET)
    @ResponseBody
    public BaseResponse adminGetNewsTypeOne() {

        ResponseBuilder custom = ResponseBuilder.custom();

        List<News> newsList = new ArrayList<>();

        try {
            newsList = newsService.getNewsTypeOne();

            custom = Constant.OBJRESULT(newsList);

        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }

        return custom.build();
    }

    /**
     * @Description:获取行业动态列表
     * @date: 11:25 2019-4-2
     * @param:
     * @return: com.jr9z.jrjz.common.BaseResponse
     */
    @RequestMapping(value = "/getTypeTwo", method = RequestMethod.GET)
    @ResponseBody
    public BaseResponse getNewsTypeTwo() {

        ResponseBuilder custom = ResponseBuilder.custom();

        List<News> newsList = new ArrayList<>();

        try {
            newsList = newsService.getNewsTypeTwo();

            custom = Constant.OBJRESULT(newsList);

        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }

        return custom.build();
    }

    /**
     * @Description:获取行业动态列表
     * @date: 11:25 2019-4-2
     * @param:
     * @return: com.jr9z.jrjz.common.BaseResponse
     */
    @RequestMapping(value = "/admin/getTypeTwo", method = RequestMethod.GET)
    @ResponseBody
    public BaseResponse adminGetNewsTypeTwo() {

        ResponseBuilder custom = ResponseBuilder.custom();

        List<News> newsList = new ArrayList<>();

        try {
            newsList = newsService.getNewsTypeTwo();

            custom = Constant.OBJRESULT(newsList);

        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }

        return custom.build();
    }

    /**
     * @Description:获取行业动态列表(分页)
     * @date: 11:25 2019-4-2
     * @param:
     * @return: com.jr9z.jrjz.common.BaseResponse
     */
    @RequestMapping(value = "/getTypeTwoPage", method = RequestMethod.GET)
    @ResponseBody
    public BaseResponse getNewsTypeTwoPage(@RequestParam Integer currPage,
                                           @RequestParam Integer pageSize) {
        ResponseBuilder custom = ResponseBuilder.custom();

        if (Objects.isNull(currPage) || currPage == 0) currPage = 1;
        if (Objects.isNull(pageSize) || pageSize == 0) pageSize = 10;
        //分页
        PageHelper.startPage(currPage, pageSize);

        List<News> newsList = new ArrayList<>();


        try {
            newsList = newsService.getNewsTypeTwo();

            Long count = newsService.getCountTypeTwo();

            if (newsList != null && count != 0) {
                custom.data(newsList).currPage(currPage).pageSize(pageSize).totalCount(count.intValue());
            } else {
                custom.failed(Constant.FAIL, Constant.ERROR_CODE);
            }

        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }

        return custom.build();
    }

    /**
     * @Description:获取行业动态列表(分页)
     * @date: 11:25 2019-4-2
     * @param:
     * @return: com.jr9z.jrjz.common.BaseResponse
     */
    @RequestMapping(value = "/admin/getTypeTwoPage", method = RequestMethod.GET)
    @ResponseBody
    public BaseResponse adminGetNewsTypeTwoPage(@RequestParam Integer currPage,
                                           @RequestParam Integer pageSize) {
        ResponseBuilder custom = ResponseBuilder.custom();

        if (Objects.isNull(currPage) || currPage == 0) currPage = 1;
        if (Objects.isNull(pageSize) || pageSize == 0) pageSize = 10;
        //分页
        PageHelper.startPage(currPage, pageSize);

        List<News> newsList = new ArrayList<>();


        try {
            newsList = newsService.getNewsTypeTwo();

            Long count = newsService.getCountTypeTwo();

            if (newsList != null && count != 0) {
                custom.data(newsList).currPage(currPage).pageSize(pageSize).totalCount(count.intValue());
            } else {
                custom.failed(Constant.FAIL, Constant.ERROR_CODE);
            }

        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }

        return custom.build();
    }

    /**
     * @Description:获取企业动态列表(分页)
     * @date: 11:25 2019-4-2
     * @param:
     * @return: com.jr9z.jrjz.common.BaseResponse
     */
    @RequestMapping(value = "/getTypeOnePage", method = RequestMethod.GET)
    @ResponseBody
    public BaseResponse getNewsTypeOnePage(@RequestParam Integer currPage,
                                           @RequestParam Integer pageSize) {
        ResponseBuilder custom = ResponseBuilder.custom();

        if (Objects.isNull(currPage) || currPage == 0) currPage = 1;
        if (Objects.isNull(pageSize) || pageSize == 0) pageSize = 10;
        //分页
        PageHelper.startPage(currPage, pageSize);

        List<News> newsList = new ArrayList<>();


        try {
            newsList = newsService.getNewsTypeOne();

            Long count = newsService.getCountTypeOne();

            if (newsList != null && count != 0) {
                custom.data(newsList).currPage(currPage).pageSize(pageSize).totalCount(count.intValue());
            } else {
                custom.failed(Constant.FAIL, Constant.ERROR_CODE);
            }

        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }

        return custom.build();
    }


    /**
     * @Description:获取企业动态列表(分页)
     * @date: 11:25 2019-4-2
     * @param:
     * @return: com.jr9z.jrjz.common.BaseResponse
     */
    @RequestMapping(value = "/admin/getTypeOnePage", method = RequestMethod.GET)
    @ResponseBody
    public BaseResponse adminGetNewsTypeOnePage(@RequestParam Integer currPage,
                                           @RequestParam Integer pageSize) {
        ResponseBuilder custom = ResponseBuilder.custom();

        if (Objects.isNull(currPage) || currPage == 0) currPage = 1;
        if (Objects.isNull(pageSize) || pageSize == 0) pageSize = 10;
        //分页
        PageHelper.startPage(currPage, pageSize);

        List<News> newsList = new ArrayList<>();


        try {
            newsList = newsService.getNewsTypeOne();

            Long count = newsService.getCountTypeOne();

            if (newsList != null && count != 0) {
                custom.data(newsList).currPage(currPage).pageSize(pageSize).totalCount(count.intValue());
            } else {
                custom.failed(Constant.FAIL, Constant.ERROR_CODE);
            }

        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }

        return custom.build();
    }

    /**
     * @Description:全部新闻
     * @date: 11:25 2019-4-2
     * @param:
     * @return: com.jr9z.jrjz.common.BaseResponse
     */
    @RequestMapping(value = "/admin/getNewsList", method = RequestMethod.GET)
    @ResponseBody
    public BaseResponse getAllNews(Integer currPage,Integer pageSize) {
        ResponseBuilder custom = ResponseBuilder.custom();

        if (Objects.isNull(currPage) || currPage == 0) currPage = 1;
        if (Objects.isNull(pageSize) || pageSize == 0) pageSize = 10;
        //分页
        PageHelper.startPage(currPage, pageSize);

        List<News> newsList = new ArrayList<>();


        try {
            newsList = newsService.getAllNews();

            Long count = newsService.getCountAll();

            if (newsList != null && count != 0) {
                custom.data(newsList).currPage(currPage).pageSize(pageSize).totalCount(count.intValue());
            } else {
                custom.failed(Constant.FAIL, Constant.ERROR_CODE);
            }

        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }

        return custom.build();
    }

    /**
     * 获取上一条新闻
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/getPrev", method = RequestMethod.GET)
    @ResponseBody
    public BaseResponse getPrev(Integer newsId) {

        News news;

        ResponseBuilder custom = ResponseBuilder.custom();

        try {

            news = newsService.getPrevNews(newsId);

//            Constant.RESULT(result);
            custom.data(news).success(Constant.SUCCESS, Constant.SUCCESS_CODE);

        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }

        return custom.build();
    }

    /**
     * 获取下一条新闻
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/getNext", method = RequestMethod.GET)
    @ResponseBody
    public BaseResponse getNext(Integer newsId) {

        News news;

        ResponseBuilder custom = ResponseBuilder.custom();

        try {

            news = newsService.getNextNews(newsId);

//            Constant.RESULT(result);
            custom.data(news).success(Constant.SUCCESS, Constant.SUCCESS_CODE);

        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }

        return custom.build();
    }
}
